<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <title>SpringCloud Demo</title>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>

<div style="text-align: right;font-size: large;">
    <i id="userPanel">...</i>
</div>

<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
    // 获取指定名称的cookie
    function getCookie(name){
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
        //遍历匹配
        for ( var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
            if (arr[0] == name){
                return arr[1];
            }
        }
        return "";
    }

    var token = getCookie("lagou_token");

    $.ajax({
        url:"../api/user/info"+"/"+token,
        type:"GET",
        beforeSend:function(xhr){
            xhr.setRequestHeader('Authorization', 'Bearer ' +token);
        },
        success:function(result,status,xhr){
            if (result){
                $("#userPanel").html(result+",欢迎登陆系统。");
            }
        },
        statusCode: {
            401:function() {
                $("#userPanel").html("未授权，请登录。");
                refreshToken();
            }
        }
    });
    
    function refreshToken() {
        var refresh_token = getCookie("refresh_token");
        if (!refresh_token){
            return;
        }
        $.ajax({
            url:"../api/user/refresh",
            type:"GET",
            beforeSend:function(xhr){
                xhr.setRequestHeader('refreshtoken',refresh_token);
            },
            success:function(result,status,xhr){
                if (!result){
                }
            }
        });
    }
});
</script>
</body>
</html>